home *** CD-ROM | disk | FTP | other *** search
/ Stolen Data 2 / Stolen Data 2.adf / Text / Vec.txt < prev   
Text File  |  1989-11-02  |  5KB  |  181 lines

  1.       ---------------- More advanced coding techniques ----------------
  2.                ----------- By Kreator of ANARCHY UK ----------
  3.                             ---- 3 D routines ----
  4.  
  5.   I am  going to  approach this  topic
  6. from a mathematical point of view, the
  7. mathematics invloved  are quite simple
  8. but if you  are not particularly adept
  9. in  this  area  dont  worry  it  isn't
  10. essential to understand the underlying
  11. theory.
  12.   Now  suppose we  are given an object
  13. to    transfer   into    a   wireframe
  14. representation  on  screen.   We  must
  15. construct a  list of coordinates which
  16. specify the vertices of the object and
  17. also a connection list which tells the
  18. computer how to  connect these points
  19. together.
  20.  eg.    A cube  has 8  vertices,  and
  21. 12 connecting sides, the vertices are
  22. as follows
  23. (50,50,50)    (-50,50,50) (-50,-50,50)
  24. (50,-50,50)   (50,50,-50) (50,-50,-50)
  25. (-50,-50,-50) (-50,50,-50)
  26.  
  27. and if these are then labelled 1-8 we
  28. have the connections as follows
  29.  1-2  2-3  3-4  4-1 5-6  6-7  7-8  8-5
  30.  1-5  2-6  3-7  4-8
  31.  
  32.  
  33.  
  34.  
  35.   If you don't believe the example try
  36. to   draw   the   cube   yourself  and
  37. visualise the coordinates. Notice that
  38. the point  (0,0,0) is at the centre of
  39. the cube, this is important because in
  40. our  rotations this  will  be the only
  41. point which remains stationary.
  42.  
  43.   When rotating the object what we are
  44. in fact doing is rotating the vertices
  45. about the fixed ORIGIN (0,0,0).  There
  46. is a mathematical theorem which states
  47. that any 3 dimensional rotation can be
  48. split into  3 individual  rotations in
  49. only  2  dimensions,  which is  a much
  50. simpler  thing to  calculate.  Now  in
  51. general  it  is   quite  difficult  to
  52. calculate  these  rotations   from  an
  53. arbitrary  3D  rotation,  but  happily
  54. enough   this   doesn't   matter  when
  55. writing   demos   because   by  simply
  56. performing  2D  rotations and  varying
  57. the  3 Angles of  rotation we  achieve
  58. an interesting effect.
  59.  
  60.   The formula for 2D rotation is given
  61. as follows,
  62.  
  63.    x = X cos(s) - Y sin(s)
  64.    y = Y cos(s) + X sin(s)
  65.  
  66.   This can easily be shown with simple
  67. trigonometry.
  68.  
  69.   These formulae enable us to rotate a
  70. point in just two dimensions,  but all
  71. we now do is to rotate the point three
  72. times in different planes.
  73.   In  other words  if we  are given  a
  74. general  point  (x,y,z)  and a,b,c are
  75. the three  angles of  rotation then to
  76. calculate the rotated point follow the
  77. procedure below (just for interest the
  78. angles  a,b,c  are  called  the  Euler 
  79. angles)
  80.  
  81.    x1 = x cos(a) - y sin(a)
  82.    y1 = y cos(a) + x sin(a)
  83.  
  84.    y2 = y1 cos(b) -  z sin(b)
  85.    z1 = z  cos(b) + y1 sin(b)
  86.  
  87.    z2 = z1 cos(c) - x1 sin(c)
  88.    x2 = x1 cos(c) + z1 sin(c)
  89.  
  90.   Then  (x2,y2,z2)  holds  the rotated
  91. coordinate.  To implement  this on the
  92. Amiga use a  Sintable which has values
  93. from  -32768 to  32767,  this  can  be
  94. reused  for  the  cosine  calculations
  95. as  cos (a) = sin (a+90 degrees).  You
  96. could code the  routine something like
  97.  
  98.     Move x,d3
  99.     Move y,d4
  100.     Move z,d5
  101.     Lea  Sin,a0
  102.     Lea  Cos,a1
  103.     Move d3,d6
  104.     Move d4,d7
  105.     Move a,d0    ;a holds 2x the angle
  106.     Move (a0,d0),d1
  107.     Move (a1,d0),d2
  108.  
  109.     Muls d2,d6
  110.     Muls d1,d7
  111.     Sub  d7,d6
  112.     Add.l d6,d6
  113.     Swap d6      ;Calculation of x1
  114.  
  115.     Muls d2,d4
  116.     Muls d1,d3
  117.     Add  d3,d4
  118.     Add.l d4,d4
  119.     Swap d4      ;Calculation of y1
  120.   etc......
  121.  
  122. Now up until now we haven't considered
  123. how  the lines  will be  drawn to  the
  124. screen, I shall assume you have access
  125. to a blitter line draw routine, if not
  126. there  is  one included  on  the disk,
  127. which is  from the  System Programmers
  128. Guide.  There are two options open now
  129. we can  leave the  coordinates as they
  130. are and  simply add a  displacement to
  131. them before  plotting the lines, or go
  132. for  the  more  realsitic technique of
  133. perspective. This invloves scaling the
  134. x,y coords.  according to how far into
  135. the screen we are. A reasonable way of
  136. doing that is as follows
  137.  
  138.      x,y,z  in d3,d4,d5
  139.  
  140.      Add    #Depth+Scale,d5
  141.      Move.l #Scale*65536,d6
  142.      Divu   d6,d5
  143.  
  144.      Muls   d5,d3 ;Scale the X coord
  145.      Add.l  d3,d3
  146.      Swap   d3
  147.  
  148.      Muls   d5,d4 ;Scale the Y coord
  149.      Add.l  d3,d4
  150.      Swap   d4
  151.  
  152.   Alternatively you can use a table of
  153. scaling values.
  154.   Now all that  remains is to plot the
  155. lines. Dont  forget up  until  now all
  156. vertices have been calculated with the
  157. origin at  (0,0)  but now we must move
  158. the origin to the centre of the screen
  159. or where ever  else you want it.  This
  160. means  adding a  displacement to  each
  161. pair of coordinates.
  162.   To  see  how  these  techniques  are
  163. implemented  I've included some source
  164. for you to examine.
  165.   These routines can easily be adapted
  166. to other purposes, eg.to create vector
  167. bobs,  use a single point for each bob
  168. and   before  plotting   sort   the  z
  169. coordinates  and  plot  the   bobs  in
  170. reverse order,  also a  simple form of
  171. hidden line removal can be implemented
  172. by   creating  a   list  of  surfaces,
  173. calculating   the  normals   to  these
  174. surfaces and if the normals point away
  175. from  you dont  plot any  lines in the
  176. surface. For an example of this see my
  177. Magnetic Fields Party Demo.
  178.    Next month I will write about sine
  179. scrollers,   Kreator .......
  180.  
  181.